//
//  JHLoginViewController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/8.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHLoginViewController.h"
#import "JHTabBarController.h"
#import "JHRegisterViewController.h"
#import "JHLoginModel.h"

@interface JHLoginViewController ()
@property (strong, nonatomic) IBOutlet UITextField *phoneTextField;
@property (strong, nonatomic) IBOutlet UIView *line1View;
@property (strong, nonatomic) IBOutlet UITextField *passWordTextField;
@property (strong, nonatomic) IBOutlet UIView *line2View;
@property (strong, nonatomic) IBOutlet UIButton *registerBtn;
@property (strong, nonatomic) IBOutlet UIView *line3View;
@property (strong, nonatomic) IBOutlet UIButton *forgetBtn;
@property (strong, nonatomic) IBOutlet UIView *line4View;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation JHLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=@"登录";
}
- (void)initUI
{
//    self.line1View.backgroundColor=JK99TextColor;
//    self.line2View.backgroundColor=JK99TextColor;
//    self.line3View.backgroundColor=JK99TextColor;
//    self.line4View.backgroundColor=JK99TextColor;
    self.loginBtn.backgroundColor=JKMainColor;
    [self.registerBtn setTitleColor:JKMainColor forState:UIControlStateNormal];
    [self.forgetBtn setTitleColor:JKMainColor forState:UIControlStateNormal];
    
}



#pragma mark---   注册
- (IBAction)registerBtnDidClick:(UIButton *)sender {
    JHRegisterViewController *vc=[[JHRegisterViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark--   忘记密码

- (IBAction)forgetBtnDidClick:(UIButton *)sender {
    
    [self showAlertOK:@"温馨提示" message:@"您好,请联系qq:3247470136 或发邮件到:englishhelp@126.com,由管理员进行修改密码"];
}


#pragma mark ---  登录
- (IBAction)loginBtnDidClik:(UIButton *)sender {
    if (self.phoneTextField.text.length==0) {
        return [DJToast showCenterToast:@"手机号码不能为空"];
    }else if (self.phoneTextField.text.length!=11){
        return [DJToast showCenterToast:@"手机号码输入错误"];
    }else if (self.passWordTextField.text.length==0){
        return [DJToast showCenterToast:@"密码不能为空"];
    }else if ([DJTool justPassword:self.passWordTextField.text]==NO)
    {
        return [DJToast showCenterToast:@"密码只能输入数字和字母"];
    }
    
    NSMutableDictionary *parmes=[NSMutableDictionary dictionary];
    parmes[@"username"]=self.phoneTextField.text;
    parmes[@"password"]=self.passWordTextField.text;
    parmes[@"deviceid"]=[MFAppTool getDeviceIMEIValue];
    NSString *urlStr=[BaseUrl stringByAppendingString:users_Login];
    [DJHttpTool postWithURL:urlStr params:parmes success:^(id json) {
        BOOL isSuccess = [json[@"success"] boolValue];
        if (isSuccess==YES) {
            [DJToast showCenterToast:json[@"msg"]];
            JHLoginModel *model=[JHLoginModel mj_objectWithKeyValues:json[@"data"]];
            [kUserDefaults setObject:model._id forKey:@"userid"];
            [kUserDefaults setObject:model.username forKey:@"username"];
            [kUserDefaults synchronize];
            [DJTool afterTime:1.5 Action:^{
                JHTabBarController *rootTabBar=[[JHTabBarController alloc]init];
                rootTabBar.selectedIndex=3;
                [UIApplication sharedApplication].keyWindow.rootViewController=rootTabBar;
                
            }];
        }
        else
        {
            [DJToast showBottomToast:json[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];
   
}

@end
